package org.zoomdev.zoom.web.rendering.impl;

import org.zoomdev.zoom.common.exceptions.CodeException;
import org.zoomdev.zoom.web.action.ActionContext;
import org.zoomdev.zoom.web.exception.StatusException;
import org.zoomdev.zoom.web.html.HtmlBuilder;
import org.zoomdev.zoom.web.view.View;

import java.io.IOException;

public final class ViewRendering extends BaseRendering {


    @Override
    protected void processNormal(ActionContext context) throws Exception {
        ((View) context.getResult()).render(context.getResponse());
    }

    @Override
    protected void processThrowable(ActionContext context, Throwable throwable) throws IOException {
        HtmlBuilder builder = new HtmlBuilder();
        if (throwable instanceof StatusException.ApiError) {
            StatusException.ApiError error = (StatusException.ApiError) context.getException();
            builder.status(error.getStatus());
        } else if (throwable instanceof CodeException) {
            builder.status(418);
        } else {
            builder.status(500);
        }
        builder.title("错误提示");
        builder.body(throwable.getMessage());
        builder.writeTo(context.getResponse());
    }
}
